<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="序列号" prop="serialNo">
        <el-input
          v-model="queryParams.serialNo"
          placeholder="请输入序列号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
<!--      <el-form-item label="检验记录编号" prop="recordCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.recordCode"-->
<!--          placeholder="请输入检验记录编号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="工单号" prop="docNo">
        <el-input
          v-model="queryParams.docNo"
          placeholder="请输入工单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
<!--      <el-form-item label="物料品号" prop="materialsNum">-->
<!--        <el-input-->
<!--          v-model="queryParams.materialsNum"-->
<!--          placeholder="请输入物料品号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="物料批次号" prop="materialsBatchCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.materialsBatchCode"-->
<!--          placeholder="请输入物料批次号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检验类型id（字典维护）" prop="typeId">-->
<!--        <el-input-->
<!--          v-model="queryParams.typeId"-->
<!--          placeholder="请输入检验类型id（字典维护）"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="产线" prop="lineId">
        <el-select v-model="queryParams.lineId" class="!w-160px" clearable placeholder="请选择产线">
          <el-option
            v-for="item in productLineList"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工序" prop="processCode">
        <el-select
          v-model="queryParams.processCode"
          class="!w-160px"
          clearable
          placeholder="请选择工序"
        >
          <el-option v-for="item in processList" :key="item.processCode" :label="item.processName" :value="item.processCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="返工状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择返工状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.OPTICORE_REWORK_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="备注" prop="remark">-->
<!--        <el-input-->
<!--          v-model="queryParams.remark"-->
<!--          placeholder="请输入备注"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="报工人员id" prop="reportUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.reportUserId"-->
<!--          placeholder="请输入报工人员id"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="报工人员姓名" prop="reportUserName">-->
<!--        <el-input-->
<!--          v-model="queryParams.reportUserName"-->
<!--          placeholder="请输入报工人员姓名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检验人员姓名" prop="creatorName">-->
<!--        <el-input-->
<!--          v-model="queryParams.creatorName"-->
<!--          placeholder="请输入检验人员姓名"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-240px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          @click="openForm('create')"-->
<!--          v-hasPermi="['opticore:rework:create']"-->
<!--        >-->
<!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
<!--        </el-button>-->
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['opticore:rework:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" width="80px" type="index" />
<!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="工单号" align="center" prop="docNo" />
      <el-table-column label="序列号" align="center" prop="serialNo" />
<!--      <el-table-column label="检验记录编号" align="center" prop="recordCode" />-->
<!--      <el-table-column label="物料品号" align="center" prop="materialsNum" />-->
<!--      <el-table-column label="物料批次号" align="center" prop="materialsBatchCode" />-->
<!--      <el-table-column label="检验类型id（字典维护）" align="center" prop="typeId" />-->
      <el-table-column label="产线" align="center" prop="lineId">
        <template #default="scope">
          <span>{{ getLineLabel(scope.row.lineId) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工序" align="center" prop="processCode"/>
      <el-table-column label="返工状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.OPTICORE_REWORK_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="备注" align="center" prop="remark" />-->
<!--      <el-table-column label="报工人员id" align="center" prop="reportUserId" />-->
<!--      <el-table-column label="报工人员姓名" align="center" prop="reportUserName" />-->
<!--      <el-table-column label="检验人员姓名" align="center" prop="creatorName" />-->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id, productLineList)"
            v-hasPermi="['opticore:rework:update']"
          >
            详情
          </el-button>
<!--          <el-button-->
<!--            link-->
<!--            type="danger"-->
<!--            @click="handleDelete(scope.row.id)"-->
<!--            v-hasPermi="['opticore:rework:delete']"-->
<!--          >-->
<!--            删除-->
<!--          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ReworkForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ReworkApi, ReworkVO } from '@/api/opticore/rework'
import ReworkForm from './ReworkForm.vue'
import {getLineDictDataList} from "@/api/system/dict/dict.data";
import {ProcessInfoApi} from "@/api/opticore/processinfo";
import {DICT_TYPE, getStrDictOptions} from "@/utils/dict";

/** 返工记录 列表 */
defineOptions({ name: 'Rework' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ReworkVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  serialNo: undefined,
  recordCode: undefined,
  orderCode: undefined,
  materialsNum: undefined,
  materialsBatchCode: undefined,
  typeId: undefined,
  lineId: undefined,
  processCode: undefined,
  status: undefined,
  remark: undefined,
  reportUserId: undefined,
  reportUserName: undefined,
  creatorName: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const productLineList = ref([])
const processList = ref([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ReworkApi.getReworkPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const fetchProductLineList = async () => {
  try {
    const result = await getLineDictDataList();
    productLineList.value = result.map(item => ({
      id: item.id,      // 产线id
      label: item.label // 产线名称
    }));
  } finally {
  }
}

function getLineLabel(lineId) {
  const line = productLineList.value.find(item => item.id === lineId);
  return line ? line.label : '';
}

const fetchProcessList = async () => {
  try {
    processList.value = await ProcessInfoApi.fetchProcessDoList()
  } finally {
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number, productLineList?:  any[]) => {
  formRef.value.open(type, id, productLineList)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ReworkApi.deleteRework(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ReworkApi.exportRework(queryParams)
    download.excel(data, '返工记录.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  fetchProcessList()
  fetchProductLineList()
})
</script>
